﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace StoryCardLogic.ImportExportData
{
    public class XmlStorySaver : IStorySaver
    {
        public readonly IFile FileManager;
        public readonly IStoryManager StoryManager;

        public XmlStorySaver(IFile fileManager, IStoryManager storyManager)
        {
            FileManager = fileManager;
            StoryManager = storyManager;
        }

        /// <summary>
        ///     Сохранить список историй в xml файл
        /// </summary>
        /// <param name="path"></param>
        public void Save(string path)
        {
            var serializer = new XmlSerializer(typeof (List<Story>));
            var stringBuilder = new StringBuilder();
            TextWriter writer = new StringWriter(stringBuilder);
            serializer.Serialize(writer, StoryManager.Stories);
            FileManager.SaveToFile(path, stringBuilder.ToString());
        }
    }
}